
package com.x52im.mall.logic.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.x52im.mall.IntentFactory;
import com.x52im.mall.ObjectFactory;
import com.x52im.mall.shop.dto.SOConsigneeInfo;
import com.x52im.rbchat.R;
import com.x52im.mall.MallGlobal;
import com.eva.android.widget.DataRender;

/**
 * 订单确认：收货信息包装实现类.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public class OrderComfirmGoodRecieverWrapper
{
	private Activity parentActivity = null;

	private SOConsigneeInfo consigneeInfo = null;

	/** 标签组件：收货信息空提示 */
	private TextView viewNullShipping = null;
	/** 标签组件：收货人姓名 */
	private TextView viewNameAndSurname = null;
	/** 标签组件：收货人国家和城市 */
	private TextView viewCountryAndCity = null;
	/** 标签组件：收货人地址 */
	private TextView viewAddr = null;
	
	private Button editAddress = null;

	/** 收货信息ui组件的父布局 */
	private ViewGroup layoutOfConsignee = null;

	public OrderComfirmGoodRecieverWrapper(Activity parentActivity)
	{
		this.parentActivity = parentActivity;

		initViews();
		initListeners();
	}

	private void initViews()
	{
		viewNullShipping = (TextView) parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_shipping_info_null);
		viewNameAndSurname = (TextView) parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_consignee_nameView);
		viewCountryAndCity = (TextView) parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_consignee_countryAndCityView);
		viewAddr = (TextView) parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_consignee_addrView);
		layoutOfConsignee = (ViewGroup) parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_consignee_LL);
		editAddress = (Button)parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_consignee_btn);
		
		viewNullShipping.setVisibility(View.VISIBLE);
		viewNameAndSurname.setVisibility(View.GONE);
		viewCountryAndCity.setVisibility(View.GONE);
		viewAddr.setVisibility(View.GONE);
	}

	private void initListeners()
	{
		editAddress.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// 当用户没用登陆时作未登陆的处理
				if(!MallGlobal.getMallProviderInstance(parentActivity).getRobotimeMall().isUserLogin())
					MallGlobal.getMallProviderInstance(parentActivity).getRobotimeMall().processForUserUnLogin(parentActivity);
				else
				{
////					Intent intent = new Intent(parentActivity, OrderConfirmShippingAddressActivity.class);
					parentActivity.startActivityForResult(IntentFactory.createOrderConfirmShippingAddressActivityIntent(
							parentActivity, consigneeInfo), OrderComfirmActivity.REQUEST_INPUT_CONSIGNEE_INFO);
				}
			}
		});
	}
	

	public ViewGroup getLayoutOfConsignee()
	{
		return layoutOfConsignee;
	}
	
	public Button getEditAddressBtn()
	{
		return editAddress;
	}

	/**
	 * 设置新的数据.
	 * 
	 * @param consigneeInfo
	 */
	public void setConsigneeInfo(SOConsigneeInfo consigneeInfo)
	{
		this.consigneeInfo = consigneeInfo;
		refreshConsigneeInfo();
	}

	public SOConsigneeInfo getConsigneeInfo()
	{
		return this.consigneeInfo;
	}

	/**
	 * 用最新的数据来刷新UI.
	 */
	private void refreshConsigneeInfo()
	{
		if (consigneeInfo != null)
		{
			viewNameAndSurname.setText(consigneeInfo.getConsignee_surname()+" " + consigneeInfo.getConsignee_name());
//			viewCountryAndCity.setText(ObjectFactory.createCountryRenderer().getNameById(consigneeInfo.getCountry_of_consignee())
//					+", " + consigneeInfo.getCity_of_consignee());
			viewAddr.setText(consigneeInfo.getAddr_of_consignee());
			// 用异步线程的目的在于：国家的render很可能还没有被缓存到本地（就需要去服务端取）
			// ，去服务端取的过程是要操作网络的，所以得用异步独立线程实现
			new AsyncTask<Object, Integer, DataRender>(){
				@Override
				protected DataRender doInBackground(Object... parems) 
				{
					return ObjectFactory.createCountryRenderer(parentActivity);
				}
				@Override
				protected void onPostExecute(DataRender render) 
				{
					if(render != null)
						viewCountryAndCity.setText(render.getNameById(consigneeInfo.getCountry_of_consignee())
								+", " + consigneeInfo.getCity_of_consignee());
				}
			}.execute();
		}
	}

	/**
	 * 数据合法性检验，保证要提交到服务端的数据不为空.
	 * 
	 * @return
	 */
	public boolean isValid()
	{
		if (consigneeInfo != null)
		{
			// String.valueOf(txtUid.getText()).trim().length()
			return __isValid(consigneeInfo.getConsignee_surname()) && __isValid(consigneeInfo.getConsignee_name())
					&& __isValid(consigneeInfo.getCity_of_consignee()) && __isValid(consigneeInfo.getAddr_of_consignee())
					&& __isValid(consigneeInfo.getCode_of_consignee()) && __isValid(consigneeInfo.getMail_of_consignee())
					&& __isValid(consigneeInfo.getPhone_of_consignee()) && __isValid(consigneeInfo.getCountry_of_consignee());
		}
		return false;
	}

	private boolean __isValid(String text)
	{
		return text != null && text.trim().length() > 0;
	}

	/**
	 * 本方法处理父Activity中的“收货信息”填写的Activity的回调.
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	public void processParentOnActivityResult(int requestCode, int resultCode, Intent data)
	{
		switch (requestCode)
		{
		// 从“收货信息”填写Activity中回调时
		case OrderComfirmActivity.REQUEST_INPUT_CONSIGNEE_INFO:
		{
			// When DeviceListActivity returns with a device to connect
			if (resultCode == Activity.RESULT_OK)
			{
				viewNullShipping.setVisibility(View.GONE);
				
				viewNameAndSurname.setVisibility(View.VISIBLE);
				viewCountryAndCity.setVisibility(View.VISIBLE); 
				viewAddr.setVisibility(View.VISIBLE);
				
				// 从回调中取出最新的收货信息数据
				SOConsigneeInfo ci = (SOConsigneeInfo) data.getExtras().getSerializable(OrderComfirmActivity.INTENT_KEY_EXTRA_CONSIGNEE$INFO$DTO);
				// 新最新的收货信息数据更新到本Activity的ui上
				setConsigneeInfo(ci);
			}
			break;
		}
		}
	}
}